<template>
  <div class="goodsindex">
    <!-- 搜索条件 -->
    <el-row :gutter="20" class="goodsindex-queryInfo">
      <!-- 商品名称搜索 -->
      <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
        <el-input
          class="goodsindex-queryInfo-li"
          v-model="queryInfo.goodsName"
          clearable
          size="small"
          placeholder="请输入商品名称"
        ></el-input>
      </el-col>
      <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
        <el-input
          class="goodsindex-queryInfo-li"
          v-model="queryInfo.storeName"
          clearable
          size="small"
          placeholder="请输入商店名称"
        ></el-input>
      </el-col>
      <!-- 商品分类搜索 -->
      <!-- <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
        <el-select
          class="goodsindex-queryInfo-li"
          v-model="queryInfo.state"
          size="small"
          clearable
          placeholder="请选择支付状态"
        >
          <el-option
            v-for="item in options.error"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-col> -->
      <el-col :xs="8" :sm="6" :md="6" :lg="4" :xl="4">
        <el-select
          class="goodsindex-queryInfo-li"
          v-model="queryInfo.orderStatus"
          size="small"
          clearable
          placeholder="订单状态"
        >
          <el-option
            v-for="item in options.status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-col>
      <el-col :xs="6" :sm="4" :md="3" :lg="2" :xl="2">
        <el-button
          type="primary"
          class="goodsindex-queryInfo-li"
          size="small"
          @click="getUserList()"
          >搜索</el-button
        >
      </el-col>
    </el-row>
    <!-- 检索结果 -->
    <el-row :gutter="20" class="goodsindex-list">
      <el-col :span="24">
        <el-table
          :data="tableData.records"
          border
          size="small"
          style="width: 100%"
        >
          <el-table-column type="index" label="序" width="50">
          </el-table-column>
          <el-table-column prop="storeName" label="门店名称" width="100">
          </el-table-column>
          <el-table-column prop="goodsName" label="商品名称" width="100">
          </el-table-column>
          <el-table-column prop="userName" label="用户" width="80">
          </el-table-column>

          <el-table-column prop="goodsNum" label="商品数量" width="80">
          </el-table-column>
          <el-table-column prop="preMoney" label="应付金额" width="100">
          </el-table-column>
          <el-table-column prop="couponMoney" label="优惠金额" width="100">
          </el-table-column>
          <el-table-column prop="payMoney" label="实际支付金额" width="100">
          </el-table-column>

          <el-table-column
            prop="state"
            :formatter="state"
            label="支付状态"
            width="100"
          >
          </el-table-column>
          <el-table-column
            :formatter="payType"
            prop="payType"
            label="支付类型"
            width="100"
          >
          </el-table-column>
          <el-table-column
            :formatter="dateFormat"
            prop="payTime"
            label="支付时间"
            width="140"
          >
          </el-table-column>
          <el-table-column
            prop="errorStatus"
            :formatter="errorStatus"
            label="订单错误状态"
            width="100"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            prop="orderStatus"
            :formatter="orderStatus"
            label="订单状态"
            width="140"
          >
          </el-table-column>
          <el-table-column
            prop="orderId"
            label="订单id"
            width="150"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column label="操作" width="220">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                @click="select = scope.row"
                :disabled="
                  scope.row.orderStatus != 0 && scope.row.orderStatus != 1
                "
                >人工确认</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 分页 -->
    <el-row :gutter="20" class="goodsindex-list">
      <el-col :span="24" class="goodsindex-page-box">
        <el-pagination
          :hide-on-single-page="true"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
        >
        </el-pagination>
      </el-col>
    </el-row>

    <!-- delect -->
    <el-dialog title="提示" :visible.sync="select" width="30%">
      <span>是否确定{{ select ? `’${select.orderId}‘` : "" }}?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="select = null">取 消</el-button>
        <el-button
          type="primary"
          @click="confirmOrder(select ? select.orderId : null)"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import moment from "moment";
export default {
  data() {
    return {
      dialogVisible: false,
      queryInfo: {
        goodsName: "",
        queryFromTime: "",
        queryToTime: "",
        storeId: "",
        state: "",
        orderStatus: "",
        page: 1,
        pageSize: 10,
      },
      select: null,
      options: {
        status: [
          {
            label: "支付未完成",
            value: 0,
          },
          {
            label: "支付完成待店家核销",
            value: 1,
          },
          {
            label: "订单完成",
            value: 2,
          },
        ],
        error: [
          {
            label: "已支付",
            value: 1,
          },
          {
            label: "未支付",
            value: 0,
          },
        ],
      },
      tableData: [],
    };
  },
  async created() {
    this.getUserList();
  },
  methods: {
    async getUserList(pageSize, pageNo) {
      let query = {
        ...this.queryInfo,
      };
      console.log(query);
      if (query.goodsName == "") delete query.goodsName;
      if (query.queryFromTime == "") delete query.queryFromTime;
      if (query.queryToTime == "") delete query.queryToTime;
      if (query.storeId == "") delete query.storeId;
      if (query.state == "") delete query.state;
      if (query.orderStatus == "") delete query.orderStatus;
      delete query.pageSize;
      delete query.page;
      delete query.storeType;
      let list = await this.http.post("/order/queryPage", {
        pageSize: pageSize,
        pageNo: pageNo,
        query: query,
      });
      this.tableData = list.result;
    },
    handleSizeChange(size) {
      this.getUserList(size);
    },
    handleCurrentChange(size) {
      this.getUserList(this.queryInfo.pageSize, size);
    },
    to(url) {
      this.$router.push({ path: url });
    },
    async confirmOrder(id) {
      if (!id) {
        this.eui.Message({ message: "请选择订单" });
        return false;
      }
      let type, msg;
      let res = await this.http.get("/order/confirm?id=" + id);
      if (!res.error) (type = "success"), (msg = "确认成功");
      else (type = "error"), (msg = "确认失败");
      this.eui.Message({
        message: msg,
        type: type,
      });
      this.select = null;
      this.getUserList();
    },
    errorStatus(row, column) {
      if (row.errorStatus === 0) return "正常";
      else return "异常";
    },
    orderStatus(row, column) {
      if (row.orderStatus === 0) return "支付未完成";
      else if (row.orderStatus === 1) return "支付完成待店家核销";
      else return "订单完成";
    },
    state(row, column) {
      if (!row.state) return "未支付";
      else return "已支付";
    },
    payType(row) {
      if (row.payType == 0) return "用户钱包";
      if (row.payType == 1) return "现金";
      if (row.payType == 2) return "微信支付";
      if (row.payType == 4) return "邀请分润";
      if (row.payType == 3) return "微信订单支付";
      //       WALLET(0, "用户钱包"),
      // CASH(1, "现金"),
      // WX_MINA(2, "微信支付"),
      // REBATE(4,"邀请分润"),
      // WX_ORDER(3,"微信订单支付");
    },
    dateFormat(row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
  },
};
</script>

<style scoped>
.goodsindex {
  width: 100%;
  min-height: 100%;
  padding: 15px;
  box-sizing: border-box;
}
/* 搜索 */
.goodsindex-queryInfo {
  margin-bottom: 10px;
}
.goodsindex-queryInfo-li {
  width: 100%;
  height: auto;
}
/* 列表 */
.goodsindex-list {
  width: 100%;
  height: auto;
  margin-bottom: 20px;
}
/* 分页 */
.goodsindex-page-box {
  width: 100%;
  height: auto;
  display: flex;
  justify-content: flex-end;
}
</style>